/**
 * author:Administrator
 * date:2013-5-21
 */
package com.roc.web.action.merinfo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionContext;
import com.roc.bean.MerinfoSearchBean;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Operators;
import com.roc.pojo.OrgBean;
import com.roc.service.IMerchantServices;
import com.roc.service.IPagenitionService;
import com.roc.service.ITerminalService;
import com.roc.service.IorgService;
import com.roc.util.Constance;
import com.roc.util.constant.ConfigProperties;
import com.roc.util.constant.Constant;
import com.roc.util.constant.MerchantStatus;
import com.roc.util.constant.Role;

/**
 * 商户启用/停用
 * @author zx-think
 *
 */
@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Namespace("/merinfoSet")
public class MerinfoSetAction extends PaginationBaseAction {
	

	private static final long serialVersionUID = 5236170890634487270L;
	
	private Logger logger=LogManager.getLogger(MerinfoSetAction.class);
	
	private Operators merchant;
	private List<OrgBean> orgList=new ArrayList<OrgBean>();
	private String[] agentList ;
	private MerinfoSetSearchCondition searchCondition;

	private List<Object> profitRsList=new ArrayList<Object>();
	
	private int dealFlag;//处理标识
	
	private String dealMid;//处理商户号
	
	@Autowired 
	IMerchantServices merchantServices;
	@Autowired
	IPagenitionService pagenitionService;
	
	@Autowired
	IorgService orgService;
	
	@Autowired
	ITerminalService terminalService;
	
	@Action(value = "merinfoSetInit", results = { @Result(name = "success", location = "/pages/merinfo/merinfoSetList.jsp"),
            @Result(name="login",location="/pages/redirect.jsp") })

	public String merinfoSetInit() throws Exception{
		try{
			init();
		}catch (Exception e) {
			// TODO: handle exception
			logger.error(e.getMessage());
		}
		return SUCCESS;
	}


	@Action(value = "merinfoSetQuery", results = { @Result(name = "success", location = "/pages/merinfo/merinfoSetList.jsp"),
			@Result(name = "error", location = "/error.jsp")})

	public String merinfoSetQuery() throws Exception{
		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		
		if(searchCondition==null){
			searchCondition=(MerinfoSetSearchCondition)getSearchConditionFromSession();
			if(searchCondition==null){
				searchCondition=new MerinfoSetSearchCondition();
			}
		}
		try {
			
			init();
			
			if(Role.AGENT.getCode().equals(op.getRoleType())){//代理商
				 List<Operators> opList=merchantServices.listAllSubMerchants(op.getId());
				 agentList= new String[opList.size()];
				 for (int i=0;i<opList.size();i++) {
					 
					Operators o=opList.get(i);
					agentList[i]=Long.toString(o.getId());
					
				}
			   searchCondition.setAgentId(agentList);
			}
			if(Role.MERCHANT.getCode().equals(op.getRoleType())){//商户
				searchCondition.setMerchantNo(op.getAccountNo());
			}
			//this.setCurrentPage(this.checkCurrentPage());
			int rows=pagenitionService.countSQL(searchCondition);
			searchRsList=pagenitionService.findAllSQL(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
			List<MerinfoSearchBean> msList=new ArrayList<MerinfoSearchBean>();
			if(searchRsList.size()>0 && searchRsList!=null){
				
				Iterator iterator=searchRsList.iterator();
				while (iterator.hasNext()) {
					Object[] obj= (Object[])iterator.next();
					MerinfoSearchBean  msb=new MerinfoSearchBean(); 
					msb.setMid(Constance.Null2EmptyTrim(obj[0]));
					msb.setMerName(Constance.Null2EmptyTrim(obj[1]));
					msb.setMerBrName(Constance.Null2EmptyTrim(obj[2]));
					msb.setMerKind(Constance.Null2EmptyTrim(obj[3]));
					msb.setFlag(MerchantStatus.valueOfCode(Integer.parseInt(Constance.Null2EmptyTrim(obj[4]))).getName());
					msb.setRsvl(Constance.Null2EmptyTrim(obj[5]));
					msb.setRsvl3(Constance.Null2EmptyTrim(obj[6]));
					msb.setInterContact(Constance.Null2EmptyTrim(obj[7]));
					msb.setPerPhone(Constance.Null2EmptyTrim(obj[8]));
					msb.setPerName(Constance.Null2EmptyTrim(obj[9]));
					msb.setCycleId(Constance.Null2EmptyTrim(obj[10]));
					msb.setCatdType(Constance.Null2EmptyTrim(obj[11]));
					msb.setCardNo(Constance.Null2EmptyTrim(obj[12]));
					msb.setAddr(Constance.Null2EmptyTrim(obj[13]));
					msb.setAreaCode(Constance.Null2EmptyTrim(obj[14]));
					msb.setAgentId(Constance.Null2EmptyTrim(obj[15]));
					msb.setFlagCode(Constance.Null2EmptyTrim(obj[4]));
					msb.setRate(Constance.Null2EmptyTrim(obj[16]));
					msb.setRateType(Constance.Null2EmptyTrim(obj[17]));
					msb.setAmt(Constance.Null2EmptyTrim(obj[18]));
					msb.setBus_lic(Constance.Null2EmptyTrim(obj[19]));
					msList.add(msb);
				}
				
				
			}
			
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(SEARCH_RS_LIST, msList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);
			
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}
	//初始化
	public void init() throws Exception{
		Operators loginOperators = (Operators) ActionContext.getContext().getSession().get(Constant.SESSION_LOGIN_MERCHANT);
		try {
			//初始化地区
			getRequest().setAttribute("areaList", ConfigProperties.getAreaCodeProperties());
			//初始化机构
			orgList=orgService.listOrgs(loginOperators);
			//初始化结算周期
			List<Object> objList=merchantServices.queryCycle();
			getRequest().setAttribute("cycList", objList);
			Map<String, String > statusMap=new TreeMap<String, String>();
			statusMap.put(Long.toString(MerchantStatus.S_0.getCode()),MerchantStatus.S_0.getName());
			statusMap.put(Long.toString(MerchantStatus.S_1.getCode()),MerchantStatus.S_1.getName());
			statusMap.put(Long.toString(MerchantStatus.S_2.getCode()),MerchantStatus.S_2.getName());
			statusMap.put(Long.toString(MerchantStatus.S_3.getCode()),MerchantStatus.S_3.getName());
			statusMap.put(Long.toString(MerchantStatus.S_4.getCode()),MerchantStatus.S_4.getName());
			statusMap.put(Long.toString(MerchantStatus.S_5.getCode()),MerchantStatus.S_5.getName());
			statusMap.put(Long.toString(MerchantStatus.S_6.getCode()),MerchantStatus.S_6.getName());
			statusMap.put(Long.toString(MerchantStatus.S_7.getCode()),MerchantStatus.S_7.getName());
			statusMap.put(Long.toString(MerchantStatus.S_10.getCode()),MerchantStatus.S_10.getName());
			statusMap.put(Long.toString(MerchantStatus.S_11.getCode()),MerchantStatus.S_11.getName());
			statusMap.put(Long.toString(MerchantStatus.S_12.getCode()),MerchantStatus.S_12.getName());
			statusMap.put(Long.toString(MerchantStatus.S_13.getCode()),MerchantStatus.S_13.getName());
			getRequest().setAttribute("statusList", statusMap);
			
		} catch (Exception e) {
			logger.error("商户查询初始化失败",e);
			throw new Exception("商户查询初始化失败");
		}
	}

	
	@Action(value = "openOrClose", results = { @Result(name = "success", type="chain", location = "merinfoSetQuery"),
			@Result(name="login",location="/pages/redirect.jsp"),
			@Result(name = "error", location = "/error.jsp")})

	public String openOrClose() throws Exception{
		
		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		
		if(op==null){
			return LOGIN;
		}
		
		//启用停用商户
		merchantServices.updateMerchantFlag(dealMid, dealFlag, op.getAccountNo());
		//终端冻结解冻
		terminalService.updateEstidinfoFlag(dealMid, dealFlag, op.getAccountNo());
		
		String msg ="";
		if(MerchantStatus.S_13.getCode()==dealFlag){
			msg = "商户:["+dealMid+"]已经成功停用";
		}else if(MerchantStatus.S_1.getCode()==dealFlag){
			msg = "商户:["+dealMid+"]已经成功启用";
		}
		
		getRequest().setAttribute("msg",msg);
		
		return SUCCESS;
		
	}
	
	public void initSomeDataForAction() {
		
	}
	@Override
	protected void dealWithTheListSpecially(List<Object> list) {
		// TODO Auto-generated method stub
		
	}
	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#getInitPageSize()
	 */
	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return Constance.PAGE_MAX_SIZE+"";
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#getSearchConditionFromSession()
	 */
	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		// TODO Auto-generated method stub
		return (MerinfoSetSearchCondition)getSession().getAttribute(SEARCH_SESSION_MERINFO_SET_QUERY);
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#getSearchConditionFromSubClass()
	 */
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		// TODO Auto-generated method stub
		return searchCondition;
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#setSearchConditionToSubClass(com.roc.page.BaseSearchCondition)
	 */
	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		this.setSearchConditionToSubClass(searchCondition);
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#setSearchToConditionSession(com.roc.page.BaseSearchCondition)
	 */
	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_SESSION_MERINFO_SET_QUERY, searchCondition);
	}

	
	public MerinfoSetSearchCondition getSearchCondition() {
		return searchCondition;
	}


	public void setSearchCondition(MerinfoSetSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}


	/**
	 * @return the merchant
	 */
	public Operators getMerchant() {
		return merchant;
	}
	/**
	 * @param merchant the merchant to set
	 */
	public void setMerchant(Operators merchant) {
		this.merchant = merchant;
	}


	/**
	 * @return the profitRsList1
	 */
	public List<Object> getProfitRsList() {
		return profitRsList;
	}


	/**
	 * @param profitRsList1 the profitRsList1 to set
	 */
	public void setProfitRsList1(List<Object> profitRsList) {
		this.profitRsList = profitRsList;
	}

	public List<OrgBean> getOrgList() {
		return orgList;
	}

	public void setOrgList(List<OrgBean> orgList) {
		this.orgList = orgList;
	}

	public int getDealFlag() {
		return dealFlag;
	}


	public void setDealFlag(int dealFlag) {
		this.dealFlag = dealFlag;
	}

	public String getDealMid() {
		return dealMid;
	}


	public void setDealMid(String dealMid) {
		this.dealMid = dealMid;
	}
	
}
